package orm

import (
	"playbook/ginsvr/src/global"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

func init() {

	//postgres for publish
	//var dsn = "host=127.0.0.1 user=wzz password=wzz dbname=playbook port=5432 sslmode=disable TimeZone=Asia/Shanghai"
	//db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)})

	//sqlite for developer
	db, err = gorm.Open(sqlite.Open("playbook.db"), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)})
}

func CleanAll() Response {
	var resp Response
	tx := db.Begin()
	cleanActivities(tx)
	cleanActivity(tx)
	//cleanAtomsvrdefine(tx)
	cleanLoop(tx)
	cleanMapping(tx)
	cleanMsgBeanMapping(tx)
	cleanParameter(tx)
	cleanParamrelation(tx)
	cleanScript(tx)
	cleanProcess(tx)
	cleanService(tx)
	cleanTransion(tx)
	cleanSubProcess(tx)
	tx.Commit()
	resp.RespCode = "000000"
	resp.RespMsg = "ok"
	return resp
}

func cleanTransion(db *gorm.DB) {

	i := db.Exec("DELETE FROM transion").RowsAffected

	global.LOG.Infof("transion表清空成功,删除数据%d条\n", i)

}

func cleanService(db *gorm.DB) {

	i := db.Exec("DELETE FROM service").RowsAffected

	global.LOG.Infof("service表清空成功,删除数据%d条\n", i)

}

func cleanProcess(db *gorm.DB) {

	i := db.Exec("DELETE FROM process").RowsAffected

	global.LOG.Infof("process表清空成功,删除数据%d条\n", i)

}

func cleanSubProcess(db *gorm.DB) {

	i := db.Exec("DELETE FROM subprocess").RowsAffected

	global.LOG.Infof("subprocess表清空成功,删除数据%d条\n", i)

}

func cleanScript(db *gorm.DB) {
	i := db.Exec("DELETE FROM script").RowsAffected

	global.LOG.Infof("script表清空成功,删除数据%d条\n", i)

}

func cleanParamrelation(db *gorm.DB) {

	i := db.Exec("DELETE FROM paramrelation").RowsAffected

	global.LOG.Infof("paramrelation表清空成功,删除数据%d条\n", i)

}

func cleanParameter(db *gorm.DB) {

	i := db.Exec("DELETE FROM parameter").RowsAffected

	global.LOG.Infof("parameter表清空成功,删除数据%d条\n", i)

}

func cleanMsgBeanMapping(db *gorm.DB) {

	i := db.Exec("DELETE FROM massagebeanmapping").RowsAffected

	global.LOG.Infof("massagebeanmapping表清空成功,删除数据%d条\n", i)

}

func cleanMapping(db *gorm.DB) {

	i := db.Exec("DELETE FROM mapping").RowsAffected

	global.LOG.Infof("mapping表清空成功,删除数据%d条\n", i)

}

func cleanLoop(db *gorm.DB) {

	i := db.Exec("DELETE FROM loop").RowsAffected

	global.LOG.Infof("loop表清空成功,删除数据%d条\n", i)

}

func cleanActivities(db *gorm.DB) {

	i := db.Exec("DELETE FROM activities").RowsAffected

	global.LOG.Infof("activities表清空成功,删除数据%d条\n", i)

}

func cleanAtomsvrdefine(db *gorm.DB) {

	i := db.Exec("DELETE FROM atomsvrdefine").RowsAffected

	global.LOG.Infof("atomsvrdefine表清空成功,删除数据%d条\n", i)

}

func cleanActivity(db *gorm.DB) {

	i := db.Exec("DELETE FROM activity").RowsAffected

	global.LOG.Infof("activity表清空成功,删除数据%d条\n", i)
}
